"use strict";
angular.module('app.jurisdiction').controller('EditActionCtrl', function($scope,$stateParams,$http,$cookieStore) {
	$scope.userid = $cookieStore.get('userid');
	//获取所有的角色
	$http.get(baseurl+'/select_idname/role').then(function(res){
		// console.log(res.data);
		if (res.data.ret == 1 && res.data.info.length > 0) {
            $scope.roles = res.data.info;
            if ($stateParams.roleid != null && $stateParams.roleid != '') {
            	$scope.role_id = $stateParams.roleid;
            	set_value();
            }
		}			
	})
	//获取所有的动作
	$http.get("api/actions.json").then(function(res){
		$scope.actions = res.data;
	})
    $scope.sel_rol = function(){
    	$(".pro_checked").prop("checked",false);
        if ($scope.role_id == '0') {
           return;
        }
        set_value();
    };
    function set_value(){
		$http.get(baseurl+"/detail/role?id=="+$scope.role_id).then(function(res){
			 // console.log(res.data);
	         var data = res.data.info.power;
	         if (data == null || data == '') {
	         	return;
	         }
	         data = JSON.parse(data);
	         for (var i = 0; i < data.length; i++) {
	         		var pid = data[i].projectid;
	         		var power = 0;
	         		power += data[i].power;
	       	 		var checkboxs = $(".project"+pid);
	       	 		var t1 = (power & 1) == 1?true:false;
	       	 		var t2 = (power & 2) == 2?true:false;
	       	 		var t3 = (power & 4) == 4?true:false;
	     	 		$(checkboxs[0]).prop("checked",t1); 
	  	 			$(checkboxs[1]).prop("checked",t2); 
	  	 			$(checkboxs[2]).prop("checked",t3); 
	         }
		});
	}
	//获取当前用户的所有项目
	$scope.allpower = [];
	$http.get(baseurl+"/select_idname/project").then(function(res){
		if (res.data.ret == 1) {
			$scope.projects = res.data.info;
			var arr = res.data.info;
			for (var i = 0; i < arr.length; i++) {
				var projectid = arr[i].id;
				var obj = {
					projectid:projectid
				}
				$scope.allpower.push(obj);
			}
		}		
	})
	// $http.get(baseurl+'/get_session').then(function(res){
	// 	console.log(res);
	// })
	// 默认未选择角色
	$scope.role_id = '0';
	// 全选
	$scope.selectAll = function(master){
		if (master == true) {
			$scope.master = true;
		}else{
			$scope.master = false;
		}
        
	}
	// 提交数据
	$scope.submit_data = function(){
       if ($scope.role_id == '0') {
       	$("#infoAlert").modal("show");
       	return;
       }
       for (var i = 0; i < $scope.allpower.length; i++) {
       	    var projectid = $scope.allpower[i].projectid;
       	    var checkboxs = $(".project"+projectid);
       	    var power = 0;
       	    for (var j = 0; j < checkboxs.length; j++) {
       	    	if($(checkboxs[j]).is(':checked')) {
       	    		switch(j){
       	    			case 0:
       	    			   power+= 1;
       	    			break;
       	    			case 1:
       	    			   power += 2;
       	    			break;
       	    			case 2:
       	    			   power += 4;
       	    			break;
       	    		}
                } //if
       	    }
			$scope.allpower[i].power = power;
       }
       // console.log($scope.allpower);
       var obj = {power:JSON.stringify($scope.allpower)};
        $http.put(baseurl+'/data/role?id=='+$scope.role_id,obj).then(function(res){
             // console.log(res.data);
             if (res.data.ret == 1) {
             	new Notify('success','修改成功'); 
             }
        });
	}
});